package com.shundr.auth.activity;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import android.app.DatePickerDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.TextureView;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.SpinnerAdapter;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.TextView;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.ActionBar.OnNavigationListener;
import com.actionbarsherlock.view.MenuItem;
import com.shundr.auth.R;
import com.shundr.auth.base.BaseActivity;
import com.shundr.auth.base.SimpleBaseActivity;
import com.shundr.auth.common.util.DateUtils;
import com.shundr.auth.common.view.CListView;
import com.shundr.auth.common.view.CListView.OnLoadMoreListener;
import com.shundr.auth.common.view.CListView.OnRefreshListener;
import com.shundr.auth.common.view.DialogChoiceBeginEndTime;
import com.shundr.auth.common.view.MyDatePickerDialog;
import com.shundr.auth.frame.constant.LContext;
import com.shundr.auth.frame.http.LHttpRequest;
import com.shundr.auth.frame.util.LLoader;
import com.shundr.auth.frame.util.LLogger;
import com.shundr.auth.frame.util.ToastHelper;
import com.shundr.auth.user.adapter.AuthOwnnerItemAdapter;
import com.shundr.auth.user.adapter.AuthUserItemAdapter;
import com.shundr.auth.user.http.UserRequest;
import com.shundr.auth.user.model.AuthOwnnerInfo;
import com.shundr.auth.user.model.UserInfo;

/**
 * AuthUserByDateActivity
 * 
 * @author Administrator
 *
 */
public class AuthUserByDateActivity extends SimpleBaseActivity implements
		View.OnClickListener {

	private int status = -1;
	private int pageNo = 1;
	private static final int PAGE_SIZE = LContext.PAGE_SIZE;

	private Date begin;
	private Date end;

	private CListView mListView;
	private AuthUserItemAdapter mAdapter;
	private EditText etBegin;
	private EditText etEnd;

	private TextView tvCount;

	private List<UserInfo> mList = new ArrayList<UserInfo>();
	private RelativeLayout mLayoutNoContent;

	private Handler mHandler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case UserRequest.FIND_STAFF_USER_LIST:
				tvCount.setText(String.format("一共有%s位用户", msg.arg1));
				List<UserInfo> infos = (List<UserInfo>) msg.obj;
				if (infos != null) {
					if (infos.size() > 0) {
						if (pageNo == 1) {
							mList.clear();
							mListView.setVisibility(View.VISIBLE);
							mLayoutNoContent.setVisibility(View.GONE);
							if (infos.size() >= PAGE_SIZE) {
								mListView.setCanLoadMore(true);
								mListView.setAutoLoadMore(true);
								mListView.setCanRefresh(true);
							} else {
								mListView.setCanLoadMore(false);
								mListView.setAutoLoadMore(false);
								mListView.setCanRefresh(false);
							}
						}
						mList.addAll(infos);
						mAdapter.notifyDataSetChanged();
						if (pageNo == 1) {
							mListView.setSelection(0);
						}
						pageNo = pageNo + 1;
					} else {
						if (pageNo == 1) {
							mListView.setVisibility(View.GONE);
							mLayoutNoContent.setVisibility(View.VISIBLE);
						} else {
							ToastHelper.toastLong(mContext, "最后一页");
						}
					}
				}
				// ProgressDialogHelper.dismiss();
				mListView.onRefreshComplete();
				mListView.onLoadMoreComplete();
				break;
			case LHttpRequest.REQUEST_FAILED:
				mListView.onLoadMoreComplete();
				mListView.onRefreshComplete();
				ToastHelper.toastShort(mContext, msg.obj.toString());
				break;

			default:
				break;
			}
			super.handleMessage(msg);
		}

	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		begin = end = new Date();

		mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
		// 生成一个SpinnerAdapter
		SpinnerAdapter adapter = ArrayAdapter.createFromResource(this,
				R.array.array_auth_status, R.layout.item_actionbar_spinner);
		// 为ActionBar设置下拉菜单和监听器
		mActionBar.setListNavigationCallbacks(adapter, new DropDownListenser());
		setContentView(R.layout.activity_user_list);

		etBegin = (EditText) findViewById(R.id.et_begin);
		etEnd = (EditText) findViewById(R.id.et_end);
		etBegin.setOnClickListener(this);
		etEnd.setOnClickListener(this);
		tvCount = (TextView) findViewById(R.id.tv_count);
		mListView = (CListView) findViewById(R.id.lv_list);
		mLayoutNoContent = (RelativeLayout) findViewById(R.id.layout_no_content);

		mAdapter = new AuthUserItemAdapter(mList, mContext);
		mListView.setAdapter(mAdapter);
		mListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				UserInfo info = mList.get(position - 1);
				Intent intent = new Intent(mContext, LoginActivity.class);
				intent.putExtra("phone", info.getUserPhone());
				startActivityForResult(intent, 500);
			}

		});

		mListView.setOnRefreshListener(new OnRefreshListener() {

			@Override
			public void onRefresh() {
				LLogger.e("OnRefreshListener");
				pageNo = 1;
				new UserRequest(mContext, mHandler).findStaffUserInfoList(
						status, -1, DateUtils.getDayBeginString(begin),
						DateUtils.getDayEndString(end), pageNo, PAGE_SIZE);
			}
		});
		mListView.setOnLoadListener(new OnLoadMoreListener() {
			@Override
			public void onLoadMore() {
				new UserRequest(mContext, mHandler).findStaffUserInfoList(
						status, -1, DateUtils.getDayBeginString(begin),
						DateUtils.getDayEndString(end), pageNo, PAGE_SIZE);
			}
		});
		// onRefresh();
	}

	public void onRefresh() {

		etBegin.setText(DateUtils.formateDateString(begin));
		etEnd.setText(DateUtils.formateDateString(end));
		pageNo = 1;
		new UserRequest(mContext, mHandler).findStaffUserInfoList(status, -1,
				DateUtils.getDayBeginString(begin),
				DateUtils.getDayEndString(end), pageNo, PAGE_SIZE);
	}

	class DropDownListenser implements OnNavigationListener {

		@Override
		public boolean onNavigationItemSelected(int itemPosition, long itemId) {
			switch (itemPosition) {
			case 0:
				status = 0;
				break;
			case 1:
				status = -1;
				break;
			case 2:
				status = 1;
				break;
			case 3:
				status = 2;
				break;
			case 4:
				status = 3;
				break;
			default:
				break;
			}
			onRefresh();
			return true;
		}

	}

	@Override
	public void onClick(View view) {

		switch (view.getId()) {
		case R.id.et_begin:
			showDatePicker(true);
			break;

		case R.id.et_end:
			showDatePicker(false);
			break;

		default:
			break;
		}

	}

	private void showDatePicker(final boolean isBegin) {
		final Calendar calendar = Calendar.getInstance();
		calendar.setTime(isBegin ? begin : end);
		MyDatePickerDialog dialog = new MyDatePickerDialog(mContext,
				new MyDatePickerDialog.OnDateSetListener() {

					@Override
					public void onDateSet(DatePicker view, int y, int m, int d) {
						calendar.set(y, m, d);
						if (isBegin) {
							if (DateUtils.isAfter(calendar.getTime(), end)) {
								ToastHelper.toastShort(mContext,
										"选择的开始时间晚于结束时间，请重新选择！");
								return;
							}
							begin = calendar.getTime();
						} else {
							if (DateUtils.isAfter(begin, calendar.getTime())) {
								ToastHelper.toastShort(mContext,
										"选择的结束时间早于开始时间，请重新选择！");
								return;
							}
							end = calendar.getTime();
						}
						onRefresh();
					}
				}, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),
				calendar.get(Calendar.DAY_OF_MONTH));

		dialog.show();
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			finish();
			break;

		default:
			break;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode,
			Intent intent) {
		super.onActivityResult(requestCode, resultCode, intent);
		switch (resultCode) {
		case 500:
			onRefresh();
			break;

		default:
			break;
		}
	}
}
